
local japi = require 'jass.japi'
local jass = require 'jass.common'
local md5 = japi['md5']
local mt = {}
local player_data = {}
mt._type = '服务器发送数据'
mt._version = '1.0.0'

mt.url = 'localhost:7888'
mt.map = '地图名'
mt.map = '地图id'
mt.port = 'player'

mt.sync_end = false

--第一个玩家
local function firstplayer()
    for a=1,12 do
        local handle = jass.Player(a-1)
        if jass.GetPlayerController(handle) == jass.MAP_CONTROL_USER and jass.GetPlayerSlotState(handle) == jass.PLAYER_SLOT_STATE_PLAYING then
            return handle
        end
    end
end

-- info = mt.base64.encode(info)
-- post_message(url, info, function(result)
--     if result then
--         result = mt.base64.decode(info)
--     end
--发送消息
local function send_data(url,post,callback)
    local info = mt.json.encode(post)
    post_message(url, info, function(result)
        if callback then
            callback(result)
        end
    end)
end

function mt.init_data(real)
    if mt.sync_end then
        return
    end
    if real and mt.json.is_json(real) then
        local data = mt.json.decode(real)
        local config = {}
        --全局处理
        for _,da in ipairs(data['config'] or {}) do
            local name = da[1]
            local text = da[3]
            config[name] = text
        end
        local table = data['user'] or {}
        for _,pda in pairs(player_data) do
            pda.data = table[pda.id]
        end
        mt.config_data = config
        mt.sync_end = true
        if mt.init_end then
            mt.init_end(true)
        end
        return
    end
end




--获取数据
function mt.get(handle,key)
    if player_data and mt.sync_end then
        local user = player_data[handle]
        return user.data[key]
    end
end

--设置数据
function mt.set(handle,key,value)
    if player_data and mt.sync_end then
        local user = player_data[handle]
        if value~=user.data[key] then
            user.data[key] = value
            user.save[key] = value
            user.allow_upload = true
        end
    end
end

--上传存档
function mt.upload(handle)
    if player_data and mt.sync_end then
        local user = player_data[handle]
        if not user or not user.allow_upload then
            return
        end
        local post = {
            uid = user.uid,
            map = mt.map,
            cmd = "魔兽修改",
            name = user.name,
            data = user.save,
        }
        user.save = {}
        user.allow_upload = nil
        
        if handle == jass.GetLocalPlayer() then
            local url = mt.url
            send_data(url, post)
        end
    end
end

--上传玩家日志
function mt.player_log(handle,logname,text)
    if player_data then
        local user = player_data[handle]
        if not user then
            return
        end
        text = mt.base64.encode(text)
        local post = {
            uid = user.uid,
            name = user.name,
            map = mt.map,
            code = true,    --使用base64编码
            cmd = "玩家日志",
            type = logname,
            data = text,
        }
        if handle == jass.GetLocalPlayer() then
            send_data(mt.url, post)
        end
    end
end

--上传游戏日志
function mt.game_log(logname,text)
    text = mt.base64.encode(text)
    local post = {
        map = mt.map,
        cmd = "游戏日志",
        code = true,    --使用base64编码
        type = logname,
        data = text,
    }
    if firstplayer() == jass.GetLocalPlayer() then
        send_data( mt.url, post)
    end
end



local users = {}
local userids = {}
--玩家参数
local group = {}
for a=1,12 do
    local handle = jass.Player(a-1)
    local uid = japi.RequestExtraStringData and japi.RequestExtraStringData(93, handle, nil, nil, false, 0, 0, 0)
    local name = japi.RequestExtraStringData and japi.RequestExtraStringData(81, handle, nil, nil, false, 0, 0, 0)
    if name==nil or name=='' then
        name = jass.GetPlayerName(handle)
    end
    if (jass.GetPlayerController(handle) == jass.MAP_CONTROL_USER and jass.GetPlayerSlotState(handle) == jass.PLAYER_SLOT_STATE_PLAYING) then
        table.insert(group,handle)
        if uid==nil or uid=='' then
            uid = ('独断test_uid%s'):format(a)
        end
    end
    local info = {
        id = a,
        handle = handle,
        uid = uid or '',
        name = name,
        save = {},
        data = {},
    }
    users[handle] = info
    users[a] = info
    userids[a] = info.uid
end
player_data = users
mt.users_id = userids


--初始化
function mt.init(callback)
    local url = mt.url
    local post = {
        user = mt.users_id,
        map = mt.map,
        cmd = "魔兽读取",
    }

    mt.sync_end = false
    send_data(url, post, function(result)
        if #result==0 then
            print('读取数据失败')
            return
        end
        if callback then
            callback(result)
        end
        if mt.sync_http then
            mt.sync_http(result)
        end
    end)
end

--拉取排名数据
function mt.init_rank(callback)
    if mt['ranks_data'] then
        return
    end
    local url = mt.url
    local post = {
        map = mt.map,
        cmd = "读取排名",
    }
    send_data(url, post, function(real)
        if real and mt.json.is_json(real) then
            local info = mt.json.decode(real)
            --排名
            local ranks = {}
            for name,data in pairs(info or {}) do
                for i,obj in ipairs(data) do
                    obj[1] = obj.name
                    obj[2] = obj.num
                end
            end
            mt['ranks_data'] = info
            if callback then
                callback()
            end
        end
    end)
end

--获取全局存档
function mt.get_config(key)
    if mt.config_data then
        return mt.config_data[key] or ''
    end
    return ''
end

--获取排名数据
function mt.get_rank(key)
    if mt['ranks_data'] then
        return mt['ranks_data'][key] or {}
    end
    return {}
end


return mt

--.\luac2.exe -o post.lua http.lua 